package com.aisile.user.service.impl;

import com.aisile.common.exception.CustomExceptionThrows;
import com.aisile.common.yongyou.YongYouCommonUtils;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.aisile.model.user.dtos.AuthDto;
import com.aisile.model.user.pojos.ApUserRealname;
import com.aisile.user.mapper.ApUserRealnameMapper;
import com.aisile.user.service.ApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Auther:yry
 * @Date:2024/4/28 0028
 * @VERSON:1.0
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private YongYouCommonUtils yongYouCommonUtils;

    @Override
    public void autoUpdateStatusById(AuthDto dto) {
        if (dto == null || dto.getId() == null) {
            return;
        }
        // 状态  0 创建中  1 待审核  2 审核失败  3.机器审核失败，待人工审核 9 审核通过
        // 根据id 查询认证信息  然后判断状态 只有状态== 1 待审核状态 才可以进行审核
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname != null && apUserRealname.getStatus().equals((short) 1)) {
            // 进行校验
            int i = 0;

            // 1. OCR （练习）
            Map map = yongYouCommonUtils.IdCardOcr(apUserRealname.getFontImage());
            if (map == null) {
                return;
            }
            // 2. 二要素
            if(apUserRealname.getIdno()==null || apUserRealname.getName()==null){
                // 审核失败
                this.updateStatusById(dto,(short)2);
                return;
            }
            if(!yongYouCommonUtils.matchIdentity(apUserRealname.getName(),apUserRealname.getIdno())){
                // 审核失败
                this.updateStatusById(dto,(short)2);
                return;
            }
            // 3. 活体检测
            int bioNo =  yongYouCommonUtils.biologicalexamination(apUserRealname.getLiveImage(),2);
            if(bioNo <0){
                // 审核失败
                this.updateStatusById(dto,(short)2);
                return;
            }else if(bioNo >0){
                // 疑似
                i++;
            }
            // 4. 人证核验
            int rzhyNo = 0;
            if(rzhyNo <0){
                // 审核失败
                this.updateStatusById(dto,(short)2);
                return;
            }else if(rzhyNo >0){
                // 疑似
                i++;
            }

            // 统一判断
            if(i == 0){
                // 审核通过
                this.updateStatusById(dto,(short)9);
            }else {
                // 疑似
                this.updateStatusById(dto, (short) 3);
            }
        }
    }

    public ResponseResult updateStatusById(AuthDto dto,Short type){
        if(dto == null || dto.getId() == null){
            CustomExceptionThrows.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if(apUserRealname == null){
            CustomExceptionThrows.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(dto.getMsg()!=null){
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealname.setStatus(type);
        this.updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }


    @Override
    public ResponseResult saveApUserRealname(ApUserRealname apUserRealname) {
        return null;
    }
}
